//
//  ZYCTourRecordStatusMarkPickerView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/17.
//

#import "ZYCTourRecordStatusMarkPickerView.h"
#import "ZYCTourRecordStatusModel.h"
@interface ZYCTourRecordStatusMarkPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong) UIView *dimView;
@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIPickerView *pickView;

@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *comfirmButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) ZYCTourRecordStatusModel *sModel;


@end
@implementation ZYCTourRecordStatusMarkPickerView

- (void)setTipString:(NSString *)tipString{
    _tipString = tipString;
    _titleLabel.text = tipString;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:0.00f];
        _sModel = self.titles[0];
        [self setupUI];
        [self setupConstraint];
        
        self.sModel  = [ZYCTourRecordStatusModel new];
        self.sModel.title = @"正常";
        self.sModel.status = @"1";
        self.sModel.textColor = HEX_RGB(@"#39A85B");
        
    }
    return self;
}

- (void)setupUI{
    [self addSubview:self.dimView];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cancelButton];
    [self.baseView addSubview:self.comfirmButton];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.line];
    [self.baseView addSubview:self.pickView];
}

- (void)setupConstraint{
    [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.baseView).offset(5.0f);
    }];
    [self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-5.0f);
        make.top.equalTo(self.baseView).offset(5.0f);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.baseView);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.baseView).offset(45.0f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.baseView);
        make.centerY.equalTo(self.baseView.mas_top).offset(22.5f);
        
    }];

}

- (UIView *)dimView{
    if (!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.3;
        _dimView.backgroundColor = [UIColor blackColor];

    }
    return _dimView;
}
- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 258)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

- (UIPickerView *)pickView {
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 218)];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kSystemFont(16.0f) titleColor:HEX_RGB(@"333333") textAlignment:ZZTextAlignmentCenter];
    }
    return _titleLabel;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EAEAEA");
    }
    return _line;
}
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc] init];
        [_cancelButton setTitleColor:HEX_RGB(kTitleMainColor) forState:UIControlStateNormal];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = kCustomFont(@"PingFangSC-Light", 14.0f);
        [_cancelButton addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [[UIButton alloc] init];
        [_comfirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:HEX_RGB(kTitleMainColor) forState:UIControlStateNormal];
        _comfirmButton.titleLabel.font = kCustomFont(@"PingFangSC-Light", 14.0f);
        [_comfirmButton addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comfirmButton;
}

- (void)clickBtnAction:(UIButton *)sender{
    [self dismiss];
    [[UIApplication sharedApplication] resignFirstResponder];
    if (self.selectStatusBlock) {
        self.selectStatusBlock(self.sModel);
    }
}



- (void)dismiss{
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 258);
    }completion:^(BOOL finished) {
        
        self.hidden = YES;
        self.dimView.hidden = YES;
        self.baseView.hidden = YES;
    }];
}
- (void)show
{
    self.hidden = NO;
    self.dimView.hidden = NO;
    self.baseView.hidden = NO;
    [UIView animateWithDuration:0.3f animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight-258, kScreenWidth, 258);
    }completion:^(BOOL finished) {
        
        
    }];
}

#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.titles.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    ZYCTourRecordStatusModel *model = self.titles[row];
    return model.title;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    ZYCTourRecordStatusModel *model = self.titles[row];
    if ((self.titles.count-1)>=row) {
        _sModel = model;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}

@end
